package com.example.petshopping.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.adapter.ProductAdapter;
import com.example.petshopping.model.Product;
import com.example.petshopping.ui.pet.PetActivity;
import com.example.petshopping.ui.subscribe.SubscribeActivity;
import com.google.android.material.chip.Chip;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    
    private RecyclerView productList;
    private List<Product> products = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 设置状态栏高度
        int statusBarHeight = getStatusBarHeight();
        findViewById(R.id.status_bar_bg).getLayoutParams().height = statusBarHeight;
        
        // 初始化视图
        initViews();
        
        // 加载商品数据
        loadProducts();
        
        // 设置商品列表
        setupProductsList();
        
        // 初始化导航菜单
        initNavMenu();
        
        // 设置点击事件
        setupClickListeners();
    }
    
    private void initViews() {
        productList = findViewById(R.id.product_list);
    }
    
    private void initNavMenu() {
        TabLayout navMenu = findViewById(R.id.nav_menu);
        navMenu.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0: // 首页 · AI
                        // 当前页面就是首页，不需要跳转
                        break;
                    case 1: // 宠物
                        startActivity(new Intent(MainActivity.this, PetActivity.class));
                        break;
                    case 2: // 水族
                        // 暂未实现，后续开发
                        Toast.makeText(MainActivity.this, "水族页面即将上线", Toast.LENGTH_SHORT).show();
                        // 恢复选中首页选项卡
                        navMenu.getTabAt(0).select();
                        break;
                    case 3: // 一口价
                        // 暂未实现，后续开发
                        Toast.makeText(MainActivity.this, "一口价页面即将上线", Toast.LENGTH_SHORT).show();
                        // 恢复选中首页选项卡
                        navMenu.getTabAt(0).select();
                        break;
                    case 4: // 同城
                        // 暂未实现，后续开发
                        Toast.makeText(MainActivity.this, "同城页面即将上线", Toast.LENGTH_SHORT).show();
                        // 恢复选中首页选项卡
                        navMenu.getTabAt(0).select();
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 不需要处理
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 再次选中当前选项卡时的处理
                if (tab.getPosition() == 0) {
                    // 如果是首页，可以执行刷新操作
                }
            }
        });
    }
    
    private void setupClickListeners() {
        // 设置自定义按钮点击事件
        Chip addTagBtn = findViewById(R.id.add_tag_btn);
        addTagBtn.setOnClickListener(v -> {
            // 跳转到订阅页面
            Intent intent = new Intent(MainActivity.this, SubscribeActivity.class);
            startActivity(intent);
        });
    }
    
    private void loadProducts() {
        // 加载多个不同的商品数据，包括不同的图片
        products.add(new Product(
            1, 
            "金毛幼犬纯种宠物狗狗活体大型犬家养拉布拉多巡回猎犬", 
            "¥1599", 
            R.drawable.canyu, 
            true,
            false, 
            "宠物之家", 
            R.drawable.default_avatar, 
            "今天20:05截拍"
        ));
        
        products.add(new Product(
            2, 
            "法斗幼犬纯种法国斗牛犬宠物狗柯基活体小型犬奶油色法牛", 
            "¥2399", 
            R.drawable.cat, 
            false,
            true, 
            "狗狗天地", 
            R.drawable.default_avatar, 
            "今天18:30截拍"
        ));
        
        products.add(new Product(
            3, 
            "秋田犬幼犬纯种日系柴犬活体宠物狗秋田幼崽忠犬八公", 
            "¥3599", 
            R.drawable.image2, 
            true,
            false, 
            "宠物乐园", 
            R.drawable.default_avatar, 
            "明天12:00截拍"
        ));
        
        products.add(new Product(
            4, 
            "英国短毛猫蓝猫活体幼猫宠物猫咪英短蓝白净梵加菲猫活物", 
            "¥2099", 
            R.drawable.clock, 
            false,
            false, 
            "猫咪之家", 
            R.drawable.default_avatar, 
            "明天15:45截拍"
        ));
    }
    
    private void setupProductsList() {
        // 设置网格布局，每行显示2列
        productList.setLayoutManager(new GridLayoutManager(this, 2));
        
        // 创建适配器并设置给RecyclerView
        ProductAdapter adapter = new ProductAdapter(this, products);
        productList.setAdapter(adapter);
        
        // 设置点击事件
        adapter.setOnProductClickListener(new ProductAdapter.OnProductClickListener() {
            @Override
            public void onProductClick(Product product, int position) {
                Toast.makeText(MainActivity.this, 
                    "点击了商品：" + product.getTitle(), 
                    Toast.LENGTH_SHORT).show();
                // 这里可以跳转到商品详情页
            }

            @Override
            public void onFavoriteClick(Product product, int position) {
                // 更新收藏状态
                product.setFavorite(!product.isFavorite());
                adapter.notifyItemChanged(position);
                
                Toast.makeText(MainActivity.this, 
                    product.isFavorite() ? "已收藏" : "已取消收藏", 
                    Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDeleteClick(Product product, int position) {
                // 从列表中移除该商品
                products.remove(position);
                adapter.notifyItemRemoved(position);
                
                Toast.makeText(MainActivity.this, 
                    "已删除商品", 
                    Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
} 